﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml;

namespace TestDoc
{
    public sealed class CommandLine
    {
        #region Constants
        #endregion

        #region Private members

        private ICollection<string> _commandlineArguments;

        #endregion

        #region Constructors
        public CommandLine(ICollection<string> commandlineArguments)
        {
            _commandlineArguments = commandlineArguments;
        }

        public CommandLine(string arguments) : this(new string[]{arguments})
        {            
        }
        #endregion

        public CommandLine Parse()
        {
            Arguments = new Dictionary<string, string>();

            string previousKey = null;
            foreach (var argument in _commandlineArguments)
            {
                if (previousKey != null)
                {
                    Arguments.Add(previousKey, argument);
                    previousKey = null;
                }
                else if (argument != null)
                {
                    if (argument.Length == 2 && argument.StartsWith("-") )
                    {
                        previousKey = argument;                         
                    }
                    else
                    {
                        string keyPairCandidate = argument.Trim();
                        if (keyPairCandidate.Length >= 2)
                        {
                            string key = keyPairCandidate.Substring(0, 2).Trim();
                            if (keyPairCandidate.StartsWith("-"))
                            {
                                string valueCandidate = argument.Trim().Substring(3).Trim();

                                if (!string.IsNullOrEmpty(key))
                                    Arguments.Add(key, valueCandidate);
                            }
                        }                        
                    }
                }
            }

            return this;
        }

        public IDictionary<string, string> Arguments
        {
            get;
            set;
        }
    }
}
